package com.zhkj.shixi.tvfilemanager;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;

import com.zhkj.shixi.tvfilemanager.Interface.IDynamicDeviceInsertReceiver;
import com.zhkj.shixi.tvfilemanager.view.GrideViewPager;

import java.io.File;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.deviceList;
import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;


public class DynamicDeviceInsertReceiver implements IDynamicDeviceInsertReceiver {
    /*
    单例模式
    * */
    private static final DynamicDeviceInsertReceiver instance = new DynamicDeviceInsertReceiver();

    public static DynamicDeviceInsertReceiver getInstance()
    {
        return instance;
    }

    private DynamicDeviceInsertReceiver() {
    }

    private IntentFilter mIntentFilter;
    private DeviceInsertReceiver mDeviceInsertReceiver;

    @Override
    public void startListener(Activity activity){
        mIntentFilter = new IntentFilter();
        mIntentFilter.addDataScheme("file");
        mIntentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        mIntentFilter.addAction(Intent.ACTION_MEDIA_EJECT);

        mDeviceInsertReceiver=new DeviceInsertReceiver(activity);
        activity.registerReceiver(mDeviceInsertReceiver,mIntentFilter);
    }
    @Override
    public void stopListener(Activity activity){
        activity.unregisterReceiver(mDeviceInsertReceiver);
    }


    class DeviceInsertReceiver extends BroadcastReceiver {
        private Activity activity;
        private DeviceInsertReceiver(Activity activity){
            this.activity=activity;
        }
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
                Toast.makeText(context, "插入", Toast.LENGTH_LONG).show();


                File device = new File(intent.getData().getPath());
                deviceList.add(device);
                if(fileManager.getCurrentURL() == null && fileManager.getType()>=0 && fileManager.getType()<=2){
                    GrideViewPager.fileList = deviceList;
                    GrideViewPager.flushViewData(GrideViewPager.fileList);
                }


            }else if(intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)){
                Toast.makeText(context, "拔出", Toast.LENGTH_LONG).show();
                //通过枚举设备池比较路径得知拔出的设备
                String USBPath=intent.getData().getPath();
                for (File device : deviceList)
                {
                    if(device.getAbsolutePath().equals(USBPath))
                    {
                        deviceList.remove(device);
                        Log.w("USB","USB Out"+"\n"+device.getAbsolutePath());

                        //TODO 调用接口重绘画某个Activity的ListView
                        if(fileManager.getCurrentURL()==null||fileManager.getCurrentURL() .contains(device.getAbsolutePath())) {
                            Log.w("out","回到USB");
                            GrideViewPager.fileList = deviceList;
                            fileManager.setCurrentURL(null);
                            GrideViewPager.flushViewData(GrideViewPager.fileList);
                        }
                        break;
                    }
                }
            }
        }
    }
}
